package com.imau.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//拦截器 /* 首页 登录页 注册页 css  image
// 统一异常的拦截
@WebFilter("/*")
public class FilterDemo implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Filter init...");
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //拦截请求增加功能
        System.out.println("Filter 拦截...");
        //1.类型转成
        HttpServletRequest request  = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        response.setHeader("Content-Type","text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        //2. 放行 传递  HttpServletRequest request HttpServletResponse respons

        filterChain.doFilter(request,response);

        //响应前 增加一些功能； 统一结果的封装
        System.out.println("Filter 响应...");
    }

    public void destroy() {
        System.out.println("Filter destroy...");
    }
}
